package com.czxy.service.impl;

import com.czxy.common.util.DateUtil;
import com.czxy.mapper.CustomsPassDetailMapper;
import com.czxy.mapper.CustomsPassMapper;
import com.czxy.mapper.QuestionMapper;
import com.czxy.mapper.RecordMapper;
import com.czxy.pojo.*;
import com.czxy.service.CustomsPassService;
import com.czxy.service.UserService;
import com.czxy.service.common.AbstractBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @author likk
 * @create 2018/9/5
 */
@Service
@Transactional
public class CustomsPassServiceImpl extends AbstractBaseService<TCustomsPass> implements CustomsPassService {

    @Autowired
    private CustomsPassMapper customsPassMapper;
    @Autowired
    private CustomsPassDetailMapper customsPassDetailMapper;
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public List<TCustomsPass> findAll() {
        TCustomsPass record = new TCustomsPass();
        record.setStatus(1); // 启用状态

        Example example = new Example(TCustomsPass.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", "1"); //启用状态

        example.orderBy("sort"); // 排序

        List<TCustomsPass> list = customsPassMapper.selectByExample(example);

        return list;
    }

    @Override
    public void saveCustomsPass(TCustomsPass customsPass) {
        // 保存关卡信息
        customsPassMapper.insert(customsPass);

        // 保存关卡详情信息
        TCustomsPassDetail detail = new TCustomsPassDetail();
        detail.setId(customsPass.getId());
        detail.setNumber(0); //关卡参与人数
        detail.setPassNumber(0); //通关人数
        detail.setGradeProgress(0.0); //年级通关率，单位：%
        detail.setClassProgress(0.0); //班级通关率，单位：%
        detail.setStatus(1); // 状态

        customsPassDetailMapper.insert(detail);
    }

    @Override
    public TCustomsPass findDetailById(Integer id, Integer userId) throws ParseException {

        TCustomsPass customsPass = customsPassMapper.selectByPrimaryKey(id);

        // 查询关卡详情
        TCustomsPassDetail customsPassDetail = customsPassDetailMapper.selectByPrimaryKey(id);
        customsPass.setCustomsPassDetail(customsPassDetail);

        // 查询当前学生，当前关卡的闯关记录
        TRecord record = new TRecord();
        record.setCustomsPassId(id); // 关卡id
        record.setStudentId(userId); // 学生id

        TRecord tRecord = recordMapper.selectOne(record);
        customsPass.setRecord(tRecord);
        // 计算关卡用时
        Date beginTime = tRecord.getBeginTime();
        Date now = new Date();
        tRecord.setSpendTime((now.getTime() - beginTime.getTime()) / 1000);
        tRecord.setSpendTimeStr(DateUtil.daysBetween(beginTime, now) + "天");

        // 根据关卡id，查询当前关卡所有的题目
        TQuestion question = new TQuestion();
        question.setCustomsPassId(id);
        question.setStatus(1);

        List<TQuestion> questionList = questionMapper.select(question);
        customsPass.setQuestionList(questionList);

        return customsPass;
    }
}
